package com.project.controller;

import com.alibaba.fastjson.JSON;
import com.project.pojo.*;
import com.project.service.PublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/public")
public class publicController {
    @Autowired
    private PublicService publicService;


    /**
     * 审批 （更改）申请
     */
    @PostMapping("/updateApply")
    @ResponseBody
    public String updateApply(@RequestBody Apply apply){
        //调用方法
        publicService.updateApply(apply);
        //返回成功表示
        return "updateSuccess";
    }

    /**
     * 查询全部申请
     */
    @PostMapping("/selectAllApply")
    public void selectAllApply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用方法查询
        List<Apply> applyList = publicService.selectAllApply();

        //将数据转换为json数据传到前端
        String jsonString = JSON.toJSONString(applyList);
        //写出数据（需要设置编码字符集）
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 查询 所有教室
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @PostMapping("/selectAllRoom")
    public void selectAllRoom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用方法查询
        List<Room> roomList = publicService.selectAllRoom();

        //将数据转换为json数据传到前端
        String jsonString = JSON.toJSONString(roomList);
        //写出数据（需要设置编码字符集）
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 查询 所有教学楼
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @PostMapping("/selectAllBuild")
    public void selectAllBuild(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用方法查询
        List<Build> buildList = publicService.selectAllBuild();

        //将数据转换为json数据传到前端
        String jsonString = JSON.toJSONString(buildList);
        //写出数据（需要设置编码字符集）
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 查询 所有公告
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @PostMapping("/selectAllNotice")
    public void selectAllNotice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用方法查询
        List<Notice> noticeList = publicService.selectAllNotice();

        //将数据转换为json数据传到前端
        String jsonString = JSON.toJSONString(noticeList);
        //写出数据（需要设置编码字符集）
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 模糊查询教学楼
     * @param request
     * @param response
     * @param buildName
     * @throws IOException
     */
    @GetMapping("/selectBuildByInfo/{buildName}")
    public void selectBuildByInfo(HttpServletRequest request, HttpServletResponse response,@PathVariable String buildName) throws IOException {
        List<Build> buildList = publicService.selectBuildByInfo(buildName);
        //将数据转换为json数据传到前端
        String jsonString = JSON.toJSONString(buildList);
        //写出数据（需要设置编码字符集）
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 模糊查询教室
     * @param request
     * @param response
     * @param buildName
     * @throws IOException
     */
    @GetMapping("/selectRoomByInfo/{buildName}")
    public void selectRoomByInfo(HttpServletRequest request, HttpServletResponse response,@PathVariable String buildName) throws IOException {
        List<Room> roomList = publicService.selectRoomByInfo(buildName);
        //将数据转换为json数据传到前端
        String jsonString = JSON.toJSONString(roomList);
        //写出数据（需要设置编码字符集）
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 模糊查询申请
     * @param request
     * @param response
     * @param relName
     * @throws IOException
     */
    @GetMapping("/selectApplyByInfo/{relName}")
    public void selectApplyByInfo(HttpServletRequest request, HttpServletResponse response,@PathVariable String relName) throws IOException {
        List<Apply> applyList = publicService.selectApplyByInfo(relName);
        //将数据转换为json数据传到前端
        String jsonString = JSON.toJSONString(applyList);
        //写出数据（需要设置编码字符集）
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 模糊查询管理员
     * @param request
     * @param response
     * @param relName
     * @throws IOException
     */
    @GetMapping("/selectAdminByInfo/{relName}")
    public void selectAdminByInfo(HttpServletRequest request, HttpServletResponse response,@PathVariable String relName) throws IOException {
        List<Admin> adminList = publicService.selectAdminByInfo(relName);
        //将数据转换为json数据传到前端
        String jsonString = JSON.toJSONString(adminList);
        //写出数据（需要设置编码字符集）
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 模糊查询教师
     * @param request
     * @param response
     * @param tName
     * @throws IOException
     */
    @GetMapping("/selectTeacherByInfo/{tName}")
    public void selectTeacherByInfo(HttpServletRequest request, HttpServletResponse response,@PathVariable String tName) throws IOException {
        List<Teacher> teacherList = publicService.selectTeacherByInfo(tName);
        //将数据转换为json数据传到前端
        String jsonString = JSON.toJSONString(teacherList);
        //写出数据（需要设置编码字符集）
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 模糊查询学生
     * @param request
     * @param response
     * @param relName
     * @throws IOException
     */
    @GetMapping("/selectUserByInfo/{relName}")
    public void selectUserByInfo(HttpServletRequest request, HttpServletResponse response,@PathVariable String relName) throws IOException {
        List<User> userList = publicService.selectUserByInfo(relName);
        //将数据转换为json数据传到前端
        String jsonString = JSON.toJSONString(userList);
        //写出数据（需要设置编码字符集）
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
}
